package com.cellulam.api.config;

import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.List;

@EnableSwagger2
@Configuration
@Profile({"dev", "qa"})
public class SwaggerConf {

    @Value("${swagger.base.package:com.cellulam.api.controller}")
    private String swaggerBasePackage;

    @Value("${swagger.title:API接口文档}")
    private String swaggerTitle;

    @Value("${swagger.title:API接口文档}")
    private String swaggerDescription;

    @Value("${swagger.terms.service.url:swagger/api-docs}")
    private String swaggerTermsOfServiceUrl;

    @Value("${swagger.version:1.0}")
    private String swaggerVersion;

    @Bean
    public Docket createRestApi() {
        ParameterBuilder ticketPar = new ParameterBuilder();
        List<Parameter> pars = Lists.newArrayList();
        ticketPar.name("access-token").description("access-token")
                .modelRef(new ModelRef("string")).parameterType("header")
                .required(false).build();
        pars.add(ticketPar.build());

        return new Docket(DocumentationType.SWAGGER_2)
                .globalOperationParameters(pars)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage(swaggerBasePackage))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title(swaggerTitle)
                .description(swaggerDescription)
                .termsOfServiceUrl(swaggerTermsOfServiceUrl)
                .version(swaggerVersion)
                .build();
    }
}
